class Manage::PhotosController < Manage::MainController
  before_filter :find_structure
  before_filter :find_photo, :only=>[:edit, :update, :destroy]
  
  # GET /manage/photos
  # GET /manage/photos.xml
  def index
    options = {:page=>params[:page], :per_page=>20, :order=>"id DESC"}
    @photos = @structure.photos.paginate(:all, options)
    
    respond_to do |format|
      format.html # render index.html
      format.xml{ render :xml => @photos }
    end
  end
  
  # GET /manage/photos/new
  def new
    @photo = @structure.photos.new
  end
  
  # POST /manage/photos
  # POST /manage/photos.xml
  def create
    @photo = @structure.photos.new(params[:photo])
    
    respond_to do |format|
    	if @photo.valid? && @photo.save
        flash[:notice] = I18n.t('flash.photo.created')
        
        format.html { redirect }
        format.xml { head :ok }
      else
      	flash.now[:error] = I18n.t('flash.photo.create_error')
      	
        format.html { render :action => "new" }
        format.xml { render :xml => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /manage/photos/1
  # PUT /manage/photos/1.xml
  def update    
    respond_to do |format|
    	if @photo.update_attributes(params[:photo])
        flash[:notice] = I18n.t('flash.photo.updated')
        
        format.html { redirect }
        format.xml { head :ok }
      else
      	flash.now[:error] = I18n.t('flash.photo.update_error')
      	
        format.html { render :action => "edit" }
        format.xml { render :xml => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /manage/photos/1
  # DELETE /manage/photos/1.xml
  def destroy
    @photo.destroy
    flash[:notice] = I18n.t('flash.photo.destroy')
    
    respond_to do |format|
      format.html { redirect }
      format.xml { head :ok }
    end
  end
  
  private
  
  def redirect
  	redirect_to manage_structure_photos_path(@structure.id)
  end
  
  def find_structure
  	@structure = Structure.find(params[:structure_id])
  end
  
  def find_photo
    @photo = Photo.find(params[:id])
  end
end
